<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    table {
      border: 1px solid black;
      border-collapse: collapse;
      border-spacing: 0;
    }

    th,
    td {
      border: 1px solid black;
      text-align: center;
      padding: 30px 50px;
    }
  </style>
  <script src="./js/vue.js"></script>
</head>

<body>
  <div id="root">
    <button @click="addtr">添加</button>
    <div v-if="goods.length>0">
      <table>
        <tr>
          <th>id</th>
          <th>商品名称</th>
          <th>商品价格</th>
          <th>商品数量</th>
          <th>操作</th>
        </tr>
        <tr v-for="(item,index) in goods" :key="item.id">
          <td>{{item.id}}</td>
          <td>{{item.name}}</td>
          <td>{{item.price}}</td>
          <td>
            <button @click="reduce(index)" :disabled="item.count<=1">-</button>
            {{item.count}}
            <button @click="addnum(index)">+</button>
          </td>
          <td><button @click="deleteObj(index)">移除</button></td>
        </tr>
        <tr v-if="add">
          <td><input type="text" v-model="id"></td>
          <td><input type="text" v-model="name"></td>
          <td><input type="text" v-model="price"></td>
          <td>
            <button @click="reduce(index)">-</button>
            <input type="text" v-model="count">
            <button @click="addnum(index)">+</button>
          </td>
          <td><button @click="addObj">确定</button></td>
        </tr>
      </table>
      <h1>总价格为:￥{{totalPrice}}元</h1>
    </div>
    <div v-else>
      <h1>购物车为空!!</h1>
      <table v-if="add">
        <tr>
          <th>id</th>
          <th>商品名称</th>
          <th>商品价格</th>
          <th>商品数量</th>
          <th>操作</th>
        </tr>
        <tr>
          <td><input type="text" v-model="id"></td>
          <td><input type="text" v-model="name"></td>
          <td><input type="text" v-model="price"></td>
          <td>
            <button @click="reduce(index)">-</button>
            <input type="text" v-model="count">
            <button @click="addnum(index)">+</button>
          </td>
          <td><button @click="addObj">确定</button></td>
        </tr>
      </table>
    </div>
  </div>


</body>
<script>
  Vue.config.productionTip = false
  new Vue({
    el: "#root",
    data() {
      return {
        id: '',
        name: '',
        price: '',
        count: '',
        add: false,
        goods: [
          { id: 1, name: "电风扇", price: 99.11, count: 1 },
          { id: 2, name: "充电宝", price: 88.22, count: 1 },
          { id: 3, name: "蓝牙耳机", price: 125.533, count: 1 },
          { id: 4, name: "小音箱", price: 315.344, count: 1 },
        ],
      }
    },
    methods: {
      addtr() {
        this.add = true
      },
      addnum(index) {
        this.goods[index].count++
      },
      reduce(index) {
        this.goods[index].count--
      },
      addObj() {
        let goodsObj = { id: this.id, name: this.name, price: this.price, count: this.count }
        this.goods.push(goodsObj)
        this.add = false
      },
      deleteObj(index) {
        this.goods.splice(index, 1)
      }
    },
    computed: {
      totalPrice() {
        let total = 0;
        for (let i = 0; i < this.goods.length; i++) {
          let items = this.goods[i];
          total += items.price * items.count;
        }
        total = total.toFixed(2)
        return total
      }
    }
  })
</script>

</html>